import React, { ReactNode } from 'react';
import { Table } from 'antd';
import { ObjectType } from '@/pages/riskControl/utils';

export default class CommonTable extends React.Component<ObjectType, ObjectType> {
  // eslint-disable-next-line react/sort-comp
  constructor(props: ObjectType) {
    super(props);
    this.state = {
      pageSize: props.pageSize, // 一页默认展示表格行数
      currentPage: props.currentPage, // 当前页码
    };
  }

  public selectedRowKeys: number[] = [];

  render(): ReactNode {
    const {
      pageSize, // 每一页展示的数据条数
      currentPage, // 当前页码
    } = this.state;
    const {
      columns,
      rowSelectionProps,
      rowKey,
      dataSource,
      handleTableChange,
      className,
      pageTotal, // 查询返回的所有数据的条数
      onPaginationChange, // 翻页器翻页
      pageSizeOptions = ['10', '20', '50', '200', '500'],
      ...others
    } = this.props;
    const rowSelection = {
      // 把选择框固定在左边
      fixed: true,
      ...rowSelectionProps,
    };
    return (
      // @ts-ignore
      <Table
        rowSelection={rowSelection}
        columns={columns}
        dataSource={dataSource}
        className={className}
        onChange={handleTableChange}
        Bordered
        pagination={{
          total: pageTotal,
          pageSize,
          current: currentPage,
          pageSizeOptions,
          onChange: onPaginationChange,
          showSizeChanger: true,
          showQuickJumper: true,
          showTotal: () =>
            `共 ${pageTotal}条记录 第 ${currentPage} / ${Math.ceil(pageTotal / pageSize)}`,
        }}
        {...others}
      />
    );
  }
}
